package com.xhj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhj.client.VodClient;
import com.xhj.entity.Video;
import com.xhj.entity.VideoInfoForm;
import com.xhj.mapper.VideoMapper;
import com.xhj.service.VideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 课程视频(EduVideo)表服务实现类
 *
 * @author makejava
 * @since 2021-02-26 12:21:46
 */
@Service()
public class VideoServiceImpl extends ServiceImpl<VideoMapper,Video> implements VideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void saveVideoInfo(VideoInfoForm videoInfoForm) {
        Video video = new Video();
        BeanUtils.copyProperties(videoInfoForm,video);
        boolean result = this.save(video);
        if (!result) throw new RuntimeException("课时添加失败！");
    }

    @Override
    public VideoInfoForm getVideoInfoById(String id) {
        Video video = this.getById(id);
        if (video == null) throw new RuntimeException("未找到对应课时");
        VideoInfoForm videoInfoForm = new VideoInfoForm();
        BeanUtils.copyProperties(video,videoInfoForm);
        return videoInfoForm;
    }

    @Override
    public void updateVideoInfoById(VideoInfoForm videoInfoForm) {
        Video video = new Video();
        BeanUtils.copyProperties(videoInfoForm,video);
        boolean result = this.updateById(video);
        if (!result) throw new RuntimeException("课时更新失败！");
    }

    @Override
    public boolean deleteById(String videoId) {
        Video video = baseMapper.selectById(videoId);
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            vodClient.deleteById(videoSourceId);
        }
        return this.removeById(videoId);
    }
}